home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / incl / LEDA.020+881 / prio.h < prev    next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  150 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  prio.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_PRIORITY_QUEUE_H
  16. #define LEDA_PRIORITY_QUEUE_H
  17.  
  18. #include <LEDA/impl/f_heap.h>
  19.  
  20. typedef f_heap_item pq_item;
  21.  
  22.  
  23. template<class ktype, class itype> 
  24.  
  25. class _CLASSTYPE priority_queue: public f_heap
  26. {
  27.  
  28. int  int_type()              const { return INT_TYPE(itype); }
  29. int  cmp(GenPtr x, GenPtr y) const
  30.                      { return compare(ACCESS(itype,x),ACCESS(itype,y)); }
  31. void clear_key(GenPtr& x)   const { Clear(ACCESS(itype,x)); }
  32. void clear_inf(GenPtr& x)   const { Clear(ACCESS(ktype,x)); }
  33. void copy_key(GenPtr& x)    const { x=Copy(ACCESS(itype,x)); }
  34. void copy_inf(GenPtr& x)    const { x=Copy(ACCESS(ktype,x)); }
  35. void print_key(GenPtr x)    const { Print(ACCESS(itype,x),cout); }
  36. void print_inf(GenPtr x)    const { Print(ACCESS(ktype,x),cout); }
  37.  
  38. public:
  39.  
  40. virtual pq_item insert(ktype k,itype i) 
  41.                               { return f_heap::insert(Convert(i),Convert(k)); }
  42.  
  43. virtual pq_item find_min()       const { return f_heap::find_min();}
  44.  
  45. virtual ktype   del_min()              { ktype x = key(find_min()); 
  46.                                          f_heap::del_min(); 
  47.                                          return x; }
  48.  
  49. virtual ktype key(pq_item x)     const { return ACCESS(ktype,f_heap::inf(x)); }
  50. virtual itype inf(pq_item x)     const { return ACCESS(itype,f_heap::key(x)); }
  51.  
  52. virtual void change_key(pq_item x,ktype k) 
  53.                                        { f_heap::change_inf(x,Convert(k)); }
  54. virtual void decrease_inf(pq_item x,itype i)
  55.                                        { f_heap::decrease_key(x,Convert(i)); }
  56.  
  57. virtual void del_item(pq_item x)       { f_heap::del_item(x); }
  58.  
  59. virtual int  size()    const { return f_heap::size(); }
  60. virtual bool empty()   const { return (size()==0) ? true : false; }
  61.  
  62. virtual pq_item first_item()          const { return f_heap::first_item(); }
  63. virtual pq_item next_item(pq_item it) const { return f_heap::next_item(it); }
  64.  
  65. priority_queue<ktype,itype>& operator=(const priority_queue<ktype,itype>& Q) 
  66. { return (priority_queue<ktype,itype>&)f_heap::operator=(Q); }
  67.  
  68.  priority_queue()  {}
  69.  priority_queue(const priority_queue<ktype,itype>& Q):f_heap(Q) {}
  70. ~priority_queue()  { f_heap::clear(); }
  71. };
  72.  
  73.  
  74. //------------------------------------------------------------------------------
  75. //
  76. // Priority queues with implementation parameter:
  77. //
  78. //   _priority_queue<keytype,inftype,prio_impl> 
  79. //
  80. //------------------------------------------------------------------------------
  81.  
  82. #define _priority_queue_class(ktype,itype,impl)\
  83. \
  84. class _CLASSTYPE _prio_class_(ktype,itype,impl) : private impl, public priority_queue<ktype,itype>\
  85. {\
  86. int int_type() const { return INT_TYPE(itype); }\
  87. \
  88. int cmp(GenPtr x, GenPtr y) const\
  89.                          { return compare(ACCESS(itype,x),ACCESS(itype,y)); }\
  90. void clear_key(GenPtr& x) const { Clear(ACCESS(itype,x)); }\
  91. void clear_inf(GenPtr& x) const { Clear(ACCESS(ktype,x)); }\
  92. void copy_key(GenPtr& x) const { x=Copy(ACCESS(itype,x)); }\
  93. void copy_inf(GenPtr& x) const { x=Copy(ACCESS(ktype,x)); }\
  94. void print_key(GenPtr x) const { Print(ACCESS(itype,x),cout); }\
  95. void print_inf(GenPtr x) const { Print(ACCESS(ktype,x),cout); }\
  96. \
  97. public:\
  98. \
  99. pq_item insert(ktype k,itype i) { return pq_item(impl::insert(Convert(i),Convert(k)));}\
  100. \
  101. pq_item find_min() const { return pq_item(impl::find_min());}\
  102. \
  103. ktype del_min() { pq_item it = find_min();\
  104.                   ktype    x = key(it);\
  105.                   del_item(it);\
  106.                   return x; }\
  107. \
  108. ktype key(pq_item x) const { return ACCESS(ktype,impl::inf(impl::item(x)));}\
  109. itype inf(pq_item x) const { return ACCESS(itype,impl::key(impl::item(x)));}\
  110. \
  111. void change_key(pq_item x, ktype k)\
  112. { impl::change_inf(impl::item(x),Convert(k)); }\
  113. \
  114. void decrease_inf(pq_item x,itype i)\
  115. { impl::decrease_key(impl::item(x),Convert(i));}\
  116. \
  117. void del_item(pq_item x) { impl::del_item(impl::item(x)); }\
  118. \
  119. int size() const { return impl::size(); }\
  120. bool empty() const { return (size()==0) ? true : false; }\
  121. \
  122. pq_item first_item() const { return pq_item(impl::first_item()); }\
  123. pq_item next_item(pq_item it) const { return pq_item(impl::next_item(impl::item(it))); }\
  124. \
  125. _prio_(ktype,itype,impl)& operator=(const _prio_(ktype,itype,impl)& Q) { return (_prio_(ktype,itype,impl)&)impl::operator=(Q); }\
  126. \
  127. _prio_class_(ktype,itype,impl)() {}\
  128. _prio_class_(ktype,itype,impl)(int n) : impl(n) {}\
  129. _prio_class_(ktype,itype,impl)(const _prio_(ktype,itype,impl)& Q)\
  130. : impl(Q) {}\
  131. \
  132. ~_prio_class_(ktype,itype,impl)() { impl::clear(); }
  133.  
  134.  
  135. #if defined(__TEMPLATE_ARGS_AS_BASE__)
  136. #define _prio_class_(a,b,c) _priority_queue
  137. #define _prio_(a,b,c) _priority_queue<a,b,c>
  138. template <class ktype, class itype, class impl> 
  139. _priority_queue_class(ktype,itype,impl)
  140. };
  141. #else
  142. #define _priority_queue(a,b,c)         name4(a,b,c,_priority_queue)
  143. #define _prio_class_(a,b,c)            name4(a,b,c,_priority_queue)
  144. #define _prio_(a,b,c)                  name4(a,b,c,_priority_queue)
  145. #define _priority_queuedeclare3(_a,_b,_c) _priority_queue_class(_a,_b,_c) };
  146. #endif
  147.  
  148. #endif
  149.